/**
 * 
 */
package fr.ece.ing4.chatapplication.api;

import java.io.InputStream;
import java.io.OutputStream;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import org.jgroups.Address;
import org.jgroups.JChannel;
import org.jgroups.Message;
import org.jgroups.Receiver;
import org.jgroups.ReceiverAdapter;
import org.jgroups.View;
import org.jgroups.protocols.UDP;


/**
 * @author enzo
 *
 */
public class TransportDriver  implements Receiver  {
	
	private String clusterName;
	JChannel drvChannel;
	private TransportReceiver transportReceiver;

	public TransportDriver( final String clusterName, final String channelName, TransportReceiver transportReceiver) {
		//Logger.debug("Forcing to IpV4 stack"); \\think of using log4j for logging important information 
		System.setProperty("java.net.preferIPv4Stack", "true"); 
		System.setProperty("jgroups.bind_addr", "127.0.0.1" );
		this.clusterName = clusterName;
    	this.transportReceiver = transportReceiver;
		try{
		drvChannel = new JChannel();
		drvChannel.setName(channelName);
		UDP protocol = (UDP)drvChannel.getProtocolStack().getTransport();
		/*if (getPort(clusterName) !=0)
		protocol.setMulticastPort(getPort(clusterName)); 
		else JOptionPane.showMessageDialog(null, "can't find a port, default is used");*/
		 drvChannel.connect(clusterName);
		drvChannel.setReceiver(this);
		drvChannel.setDiscardOwnMessages(true);
		}
		catch (Exception e){
		//Logger.error("<<class Abstract Channel Driver>>can not create channel ");
		} }

	@Override
	public void getState(OutputStream arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void receive(Message arg0) {
		// TODO Auto-generated method stub
		System.out.println("test");
		transportReceiver.receiveChatMessage(arg0);
		
	}

	@Override
	public void setState(InputStream arg0) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void block() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void suspect(Address arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void unblock() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void viewAccepted(View arg0) {
		// TODO Auto-generated method stub
		transportReceiver.printView(arg0);
		transportReceiver.onViewChanged(arg0);
		
		
	}
	public void setName(String name)
	{
		drvChannel.setName(name);
	}
	private int getPort(String clusterName)
	{
		return 4000+clusterName.hashCode();
		
	}
	public Address getAddress()
	{
		return drvChannel.getAddress();
	}
	public void sendMessage(ChatMessage msg)
	{
		//Message m = new Message(msg.getDestination(),msg);
		Message m = new Message();
		System.out.println(""+m.getDest());
		m.setDest(msg.getDestination());
		m.setObject(msg);
		try {
			if(msg.getDestination()==null) 
				drvChannel.send(m); 
			else drvChannel.send(msg.getDestination(), msg);
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void disconnect()
	{
		drvChannel.disconnect();
	}
	
}
